package com.credithc.eagleeye.config;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.stereotype.Component;
/**
 * 描 述: <br/>
 * 作 者: zhangjin<br/>
 * 创 建：2017年05月11日<br/>
 * 版 本：v1.0.0<br>
 * <p>
 * 历 史: (版本) 作者 时间 注释 <br/>
 */
@Component
public class CustomizeTomcatFactory extends TomcatEmbeddedServletContainerFactory {
    @Value("${server.tomcat.min-threads}")
    private int minSpareThreads=20;
    @Value("${server.tomcat.max-connections}")
    private int maxConnections=10000;
    @Value("${server.tomcat.connection-timeout}")
    private int connectionTimeout=30000;

    @Override
    public EmbeddedServletContainer getEmbeddedServletContainer(ServletContextInitializer... initializers) {
        return super.getEmbeddedServletContainer(initializers);
    }

    @Override
    protected void customizeConnector(Connector connector) {
        super.customizeConnector(connector);
        Http11NioProtocol http11NioProtocol = (Http11NioProtocol) connector.getProtocolHandler();
        //设置端口与最大线程数用spring内置的配置属性即可
        /*
            connector.setPort(8989);
            http11NioProtocol.setMaxThreads(500);
        */
        //设置初始线程数  最小空闲线程数
        http11NioProtocol.setMinSpareThreads(minSpareThreads);
        //设置超时
        http11NioProtocol.setConnectionTimeout(connectionTimeout);
        //设置最大连接数
        http11NioProtocol.setMaxConnections(maxConnections);
    }
}
